package com.iot.utils;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.req.message.harmony.HarmonyDTO;
import com.getui.push.v2.sdk.dto.req.message.harmony.HarmonyNotification;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Map;


/***
 * 个推工具包
 */
@Slf4j
public class AppPushUtils {
    private static PushApi pushApi;

    private static String APP_ID = "eHBnZOsVtk5ZdGOEE9e1w";

    private static String APP_KEY = "noz3kLUaQmAucEFGRSPPl8";

    private static String APP_SECRET = "2Tu5UzPb2w8ALhWbOweAf1";

    private static String IOS_BUNDLE_ID = "com.xincore.tech.pushtest.NotificationService";


    private static String MASTER_SECRET = "0Dc5v0micR815MtLzUI506";

    private static String BASE_URL = "https://restapi.getui.com/v2/";

    public static void sendMessage(String cid, String title, String content) {
        if (StringUtils.isEmpty(cid)) {
            return;
        }
        // 设置httpClient最大连接数，当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置
        apiConfiguration.setAppId(APP_ID);
        apiConfiguration.setAppKey(APP_KEY);
        apiConfiguration.setMasterSecret(MASTER_SECRET);
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀
        apiConfiguration.setDomain(BASE_URL);
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        pushApi = apiHelper.creatApi(PushApi.class);

        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTransmission(content);
        //pushMessage.setTransmission("{\"pageRoute\" : \"/me/messageList?id=1234\",\"title\" : \"透传Title\",\"msg\" : \"透传Msg\"}");
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        //pushMessage.setNotification(notification);
        //notification.setTitle(title);
        //notification.setBody(content);
        //notification.setClickType("url");
        //notification.setUrl("https://www.getui.com");
        /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/

        /**** 设置厂商相关参数 ****/
        PushChannel pushChannel = new PushChannel();
        pushDTO.setPushChannel(pushChannel);
        /*配置安卓厂商参数*/
        AndroidDTO androidDTO = new AndroidDTO();
        pushChannel.setAndroid(androidDTO);
        Ups ups = new Ups();
        androidDTO.setUps(ups);
        ThirdNotification thirdNotification = new ThirdNotification();
        ups.setNotification(thirdNotification);
        thirdNotification.setTitle(title);
        thirdNotification.setBody(content);
        thirdNotification.setClickType("url");
        thirdNotification.setUrl("https://www.getui.com");
        // 两条消息的notify_id相同，新的消息会覆盖老的消息，取值范围：0-2147483647
        // thirdNotification.setNotifyId("11177");
        /*配置安卓厂商参数结束，更多参数请查看文档或对象源码*/

        /*设置ios厂商参数*/
        IosDTO iosDTO = new IosDTO();
        pushChannel.setIos(iosDTO);
        // 相同的collapseId会覆盖之前的消息
        iosDTO.setApnsCollapseId(String.valueOf(new Date().getTime()));
        Aps aps = new Aps();
        iosDTO.setAps(aps);
        Alert alert = new Alert();
        aps.setAlert(alert);

        alert.setTitle(title);
        alert.setBody(content);

        /*设置ios厂商参数结束，更多参数请查看文档或对象源码*/

        /*配置鸿蒙厂商参数*/
        HarmonyDTO harmonyDTO = new HarmonyDTO();
        pushChannel.setHarmony(harmonyDTO);
        HarmonyNotification harmonyNotification = new HarmonyNotification();
        harmonyDTO.setNotification(harmonyNotification);
        harmonyNotification.setTitle(title);
        harmonyNotification.setBody(content);
        harmonyNotification.setCategory("MARKETING");
        harmonyNotification.setClickType("want");
        harmonyNotification.setWant("{\"deviceId\":\"\",\"bundleName\":\"com.getui.push\",\"abilityName\":\"TestAbility\",\"uri\":\"https://www.test.com:8080/push/test\",\"action\":\"com.test.action\",\"parameters\":{\"name\":\"Getui\",\"age\":12}}");
        /*配置鸿蒙厂商参数结束，更多参数请查看文档或对象源码*/

        /*设置接收人信息*/
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(cid);
        /*设置接收人信息结束*/
        /**** 设置厂商相关参数，更多参数请查看文档或对象源码 ****/

        // 进行cid单推
        //pushApi.pushToSingleByCid(pushDTO);
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            // success
            log.info("push success" + apiResult.getData());
        } else {
            // failed
            log.info("push failed code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
    }

    public static void sendJsonMessage(String cid, Long fid, String title, String content) {
        if (StringUtils.isEmpty(cid)) {
            return;
        }
        // 设置httpClient最大连接数，当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置
        apiConfiguration.setAppId(APP_ID);
        apiConfiguration.setAppKey(APP_KEY);
        apiConfiguration.setMasterSecret(MASTER_SECRET);
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀
        apiConfiguration.setDomain(BASE_URL);
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        pushApi = apiHelper.creatApi(PushApi.class);

        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
//        pushMessage.setTransmission(content);
        pushMessage.setTransmission("{\"pageRoute\" : \"/me/following/requestBandLoc?id=" + fid + "\",\"title\" : \"V Band\",\"msg\" : \"" + content + "\"}");
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        //pushMessage.setNotification(notification);
        //notification.setTitle(title);
        //notification.setBody(content);
        //notification.setClickType("url");
        //notification.setUrl("https://www.getui.com");
        /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/

        /**** 设置厂商相关参数 ****/
        PushChannel pushChannel = new PushChannel();
        pushDTO.setPushChannel(pushChannel);
        /*配置安卓厂商参数*/
        AndroidDTO androidDTO = new AndroidDTO();
        pushChannel.setAndroid(androidDTO);
        Ups ups = new Ups();
        androidDTO.setUps(ups);
        ThirdNotification thirdNotification = new ThirdNotification();
        ups.setNotification(thirdNotification);
        thirdNotification.setTitle(title);
        thirdNotification.setBody(content);
        thirdNotification.setClickType("url");
        thirdNotification.setUrl("https://www.getui.com");
        // 两条消息的notify_id相同，新的消息会覆盖老的消息，取值范围：0-2147483647
        // thirdNotification.setNotifyId("11177");
        /*配置安卓厂商参数结束，更多参数请查看文档或对象源码*/

        /*设置ios厂商参数*/
        IosDTO iosDTO = new IosDTO();
        pushChannel.setIos(iosDTO);
        // 相同的collapseId会覆盖之前的消息
        iosDTO.setApnsCollapseId(String.valueOf(new Date().getTime()));
        Aps aps = new Aps();
        iosDTO.setAps(aps);
        Alert alert = new Alert();
        aps.setAlert(alert);

        alert.setTitle(title);
        alert.setBody(content);

        /*设置ios厂商参数结束，更多参数请查看文档或对象源码*/

        /*配置鸿蒙厂商参数*/
        HarmonyDTO harmonyDTO = new HarmonyDTO();
        pushChannel.setHarmony(harmonyDTO);
        HarmonyNotification harmonyNotification = new HarmonyNotification();
        harmonyDTO.setNotification(harmonyNotification);
        harmonyNotification.setTitle(title);
        harmonyNotification.setBody(content);
        harmonyNotification.setCategory("MARKETING");
        harmonyNotification.setClickType("want");
        harmonyNotification.setWant("{\"deviceId\":\"\",\"bundleName\":\"com.getui.push\",\"abilityName\":\"TestAbility\",\"uri\":\"https://www.test.com:8080/push/test\",\"action\":\"com.test.action\",\"parameters\":{\"name\":\"Getui\",\"age\":12}}");
        /*配置鸿蒙厂商参数结束，更多参数请查看文档或对象源码*/

        /*设置接收人信息*/
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(cid);
        /*设置接收人信息结束*/
        /**** 设置厂商相关参数，更多参数请查看文档或对象源码 ****/

        // 进行cid单推
        //pushApi.pushToSingleByCid(pushDTO);
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            // success
            log.info("push success" + apiResult.getData());
        } else {
            // failed
            log.info("push failed code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
    }
}